import {
  commonOrg,
  commonCj,
  commonGx,
  commonPz,
  getSysDict
} from '@/api/common';

const state = {
  cjOption: [],
  orgOption: [],
  gxOption: [],
  pzOption: [],
  orderTypeOption: [],
  branchOption: [],
  bcOption: [],
  wtOption: [],
  groupOption: [],
  kqOption: [],
  frOption: [],
  qfOption: [],
  dhOption: []
};

const mutations = {
  setOption: function(state, val) {
    state[val.type + 'Option'] = val.data;
  }
};

const actions = {
  getOrgOption({ commit }) {
    return new Promise((resolve, reject) => {
      commonOrg()
        .then(res => {
          if (res.code == 1) {
            let { data } = res;
            commit('setOption', { data: data, type: 'org' });
            resolve;
          }
        })
        .catch(error => {
          reject(error);
        });
    });
  },
  getCjOption({ commit }) {
    return new Promise((resolve, reject) => {
      commonCj()
        .then(res => {
          if (res.code == 1) {
            let { data } = res;
            commit('setOption', { data: data, type: 'cj' });
            resolve;
          }
        })
        .catch(error => {
          reject(error);
        });
    });
  },
  getGxOption({ commit }) {
    return new Promise((resolve, reject) => {
      commonGx()
        .then(res => {
          if (res.code == 1) {
            let { data } = res;
            commit('setOption', { data: data, type: 'gx' });
            resolve;
          }
        })
        .catch(error => {
          reject(error);
        });
    });
  },
  getPzOption({ commit }) {
    return new Promise((resolve, reject) => {
      commonPz()
        .then(res => {
          if (res.code == 1) {
            let { data } = res;
            commit('setOption', { data: data, type: 'pz' });
            resolve;
          }
        })
        .catch(error => {
          reject(error);
        });
    });
  },
  // 数据字典通用接口查询
  getDict({ commit }, { dict, name }) {
    return new Promise((resolve, reject) => {
      getSysDict(dict)
        .then(res => {
          if (res.code == 1) {
            let { data } = res;
            commit('setOption', { data: data.dataList, type: name });
            resolve();
          }
        })
        .catch(error => {
          reject(error);
        });
    });
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
